package com.nwpu.bigdata.service.impl;

import java.util.ArrayList;

import java.util.List;
import org.influxdb.dto.QueryResult;
import org.influxdb.dto.QueryResult.Result;
import org.influxdb.dto.QueryResult.Series;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Service;

import com.nwpu.bigdata.dto.Common;
import com.nwpu.bigdata.service.MemoryService;
import com.nwpu.bigdata.util.impl.InfluxDBUtilImpl;
import com.nwpu.bigdata.util.impl.InfluxdbService;



/**
 *@version:2018年5月28日下午8:08:31
*/
/**
 * @author Administrator
 *
 */
@Service
@ImportResource(locations= {"classpath:/xmlConfig/spring/applicationContext-util.xml"})
public class MemoryServiceImpl implements MemoryService {
	@Autowired
	private InfluxDBUtilImpl influDBClient;
	
	@Value("${retentionPolicy.cluster}")
	private String retentionPolicy;
	
	@Value("${inluxdb.k8s}")
	private String dbname;
	
	public List<Common> MemoryNodeMetrics(){
		//和k8s数据库建立连接
		
	    InfluxdbService service = new InfluxdbService(dbname,retentionPolicy,influDBClient.builder());
	    if(service.equals(null))
	    {
	    	return null;
	    }
	    //查询出每一个节点的memory利用率
	    
	    QueryResult queryResult = service.query("SELECT value FROM \"memory/node_utilization\" WHERE time > now() - 10m group by nodename");//查询的某个指标
	    if(queryResult.getResults() == null) {
			System.out.println("the result is empty, end query!");
			return null; 
		}
	    List<Common> lists = new ArrayList<Common>();
		for(Result result : queryResult.getResults()){
			List<Series> series = result.getSeries();
			if(series==null){
				return null;
			}
			
			for(Series serie : series) {
				Common commonNode = new Common();
				List<Double> values = new ArrayList<Double>();
				String nodename = serie.getTags().get("nodename");
				for(int i = 0; i < serie.getValues().size(); i++){
					if(serie.getValues().get(i).get(1) == null){
						values.add(i,0.0);
					}else{
						
					values.add(i, (Double)serie.getValues().get(i).get(1));}
				}
			    
				commonNode.setNodename(nodename);
				commonNode.setValues(values);
                lists.add(commonNode);
			}
			
		}
		influDBClient.builder().close();
	    return lists;
	}
	
	
public List<Common> softwareMemoryMetrics(String softwareName){
		
	    InfluxdbService service = new InfluxdbService(dbname,retentionPolicy,influDBClient.builder());
	    if(service.equals(null))
	    {
	    	return null;
	    }
	    //通过软件名查询装有该组件的pod的cpu使用大小
	    QueryResult queryResult = null;
	    //if(softwareName=="mysql" || softwareName=="nosqldb" || softwareName=="iotdb-server"){
	    
	    if(softwareName=="iotdb" || softwareName=="iotdb-client" || softwareName=="iotdb-grafana" ||softwareName=="kvstore" || softwareName=="kvstore-v1" || softwareName=="kvstore-v1-0" || softwareName=="kvstore-v1-1" || softwareName=="kvstore-v1-2" || softwareName=="ingraphdb" || softwareName=="ingraphdb-v1"  || softwareName=="ingraphdb-v1-0" || softwareName=="ingraphdb-v1-1" || softwareName=="ingraphdb-v1-2") {
	    	queryResult = service.query("SELECT value FROM \"memory/usage\" WHERE time > now() - 10m and \"pod_name\" =~ /"+softwareName+"/ and type  = 'pod' group by pod_name");//查询的某个指标
	    }
	    /*else if(softwareName=="mysql" || softwareName=="nosqldb" || softwareName=="iotdb-server" ){
	    	queryResult = service.query("SELECT value FROM \"memory/usage\" WHERE time > now() - 10m and pod_name = '"+softwareName+"' and type  = 'pod' group by pod_name");	
	    }*/
	    else{
	    	queryResult = service.query("SELECT value FROM \"memory/usage\" WHERE time > now() - 10m and \"pod_name\" =~ /"+softwareName+"/ and type  = 'pod' group by pod_name");//查询的某个指标
	    }
	    if(queryResult.getResults() == null) {
			System.out.println("the result is empty, end query!");
			return null; 
		}
	    List<Common> lists = new ArrayList<Common>();
	    for(Result result : queryResult.getResults()){
			List<Series> series = result.getSeries();
			if(series==null){
				return null;
			}
			for(Series serie : series) {
				Common commonNode = new Common();
				List<Double> values = new ArrayList<Double>();
				softwareName = serie.getTags().get("pod_name");
				System.out.println(serie);
				for(int i = 0; i < serie.getValues().size(); i++){
					if(serie.getValues().get(i).get(1) == null){
						values.add(i,0.0);
					}else{
					values.add(i,(Double) serie.getValues().get(i).get(1));}
				}
			    
				commonNode.setNodename(softwareName);
				commonNode.setValues(values);
                lists.add(commonNode);
			}
			
		}
	    
	    influDBClient.builder().close();
	   
		return lists;
	}
}
